package com.yaozheng.vocationaltraining.adapter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.iview.IItemView;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.MemoryWealthItemView;
import com.yaozheng.vocationaltraining.view.MemoryWealthItemView_;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 记忆财富(你该复习课程)
 * 
 * @author Administrator
 * 
 */
public class MemoryWealthAdapter extends BaseAdapter {
	private BaseActivity baseActivity;

	JSONArray dataList;

	IItemView iItemView;

	public MemoryWealthAdapter(BaseActivity baseActivity, JSONArray dataList,
			IItemView iItemView) {
		super();
		this.baseActivity = baseActivity;
		this.dataList = dataList;
		this.iItemView = iItemView;
	}

	public JSONArray getDataList() {
		return dataList;
	}

	@Override
	public int getCount() {
		return TypeUtils.getJsonArraySize(dataList);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 得到最后id
	 * 
	 * @return
	 */
	public int getLastId() {
		if (dataList != null && TypeUtils.getJsonArraySize(dataList) > 0) {
			JSONObject itemData = TypeUtils.getJsonObject(dataList,
					TypeUtils.getJsonArraySize(dataList) - 1);
			if (itemData != null) {
				return TypeUtils.getJsonInteger(itemData, "id", -1);
			}
		}
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MemoryWealthItemView memoryWealthItemView;
		if (convertView == null) {
			memoryWealthItemView = MemoryWealthItemView_.build(baseActivity);
			memoryWealthItemView.init(iItemView);
		} else {
			memoryWealthItemView = (MemoryWealthItemView) convertView;
		}
		memoryWealthItemView.loadData(position,
				TypeUtils.getJsonObject(dataList, position));
		return memoryWealthItemView;
	}

}
